var AlipayManager = Vue.component('alipayManager', function (resolve, reject) {
    axios.get('views/alipay-manager/alipayManager.html?timestamp'+timestamp).then(function (res) {
        resolve({
            template: res.data,
            data(){
                return {
                    needAdminRole:CommonUtils.checkRole('admin')||CommonUtils.checkRole('access'),
                    needAliMgrRole:CommonUtils.checkRole("alipay"),
                    loading: false,
                    tableData: [],
                    dialogFormVisible: false,
                    form: {akjMerchants:{id:""}},
                    pageInfo:{
                        currentPage:1,
                        pageSize:10,
                        totalNum:10,
                        srch_EQ_id:[],
                        srch_LIKE_name:""
                        // srch_EQ_delete:false
                    },
                    allLifes:[],
                    srchLifeIds:[],
                    searchMerchants:"",
                    editForm:{akjMerchants:{}},
                    showEditWin:false,
                    showDetails:false,
                    detailData:{
                        createBy:{
                            name:""
                        }
                    },
                    formLabelWidth: '120px',
                    copyForm:{},
                    showCopy:false,
                    merchants:[]
                }
            },
            methods:{

                enableLife(row){
                    if(!CommonUtils.checkRole('admin')&&!CommonUtils.checkRole('access')){
                        this.$alert('权限不足', '', {
                            confirmButtonText: '确定',
                        });
                        return;
                    }
                    this.$confirm('确定重新启用此生活号吗？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        httpInvoker.post(APIs.alipay.enableLife,{id:row.id}).then(res =>{
                            if(res.status == 200 && res.data.status == '00'){
                                this.$alert('启用成功', '', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        this.initData();
                                    }

                                });
                            }else{
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        })
                    }).catch(() => {

                    });
                },
                openDetail:function(row){
                    this.detailData = row;
                    if(CommonUtils.checkNull(this.detailData.createBy)){
                        this.detailData.createBy={name:''};
                    }
                    this.showDetails=true;
                    setTimeout(function () {
                        jQuery(".el-form-item__content").css("line-height","20px");
                        jQuery(".el-form-item__label").css("line-height","20px");
                    },300)
                },
                handleCurrentChange: function(currentPage){
                    this.pageInfo.currentPage = currentPage;
                    this.initData();
                },
                initData:function(){

                    httpInvoker.get(APIs.alipay.querySrchLifes,{})
                        .then(res =>{
                            // console.log(res.data);
                            if(res.status == 200 && res.data.status == '00'){
                                this.allLifes = res.data.data.allLifes;
                                this.merchants = res.data.data.merchants?res.data.data.merchants:this.merchants;
                                if(CommonUtils.checkNull(this.srchLifeIds)){
                                    this.pageInfo.srch_EQ_id = CommonUtils.alilifeSrchLifeIds(this.allLifes);
                                }else{
                                    this.pageInfo.srch_EQ_id = this.srchLifeIds;
                                }
                                if( this.searchMerchants){
                                    this.pageInfo['srch_JOINEQ_akjMerchants.id'] = this.searchMerchants;
                                }
                                httpInvoker.get(APIs.alipay.query,{
                                    params:this.pageInfo,
                                    paramsSerializer: params => {
                                        return Qs.stringify(params, { indices: false })
                                    }
                                }).then(res =>{
                                    this.loading = false;
                                    // console.log(res.data);
                                    if(res.status == 200 && res.data.status == '00'){
                                        this.tableData = res.data.data.page.content;

                                        this.pageInfo.totalNum=res.data.data.page.totalElements;
                                    }else{
                                        this.$alert(res.data.msg, '', {
                                            confirmButtonText: '确定'
                                        });
                                    }
                                })

                            }else{
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        })

                },
                openFormWin(event){
                    if(!CommonUtils.checkRole('admin')&&!CommonUtils.checkRole('access')){
                        this.$alert('权限不足', '', {
                            confirmButtonText: '确定',
                        });
                        return;
                    }
                    this.form = {akjMerchants:{}};
                    this.dialogFormVisible=true;
                },
                submitForm(event){

                    httpInvoker.post(APIs.alipay.add,this.form).then(res =>{
                        if(res.status == 200 && res.data.status == '00'){
                            this.dialogFormVisible = false;
                            this.$alert('创建成功', '', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    this.initData();
                                }

                            });
                        }else{
                            this.loading = false;
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                },
                submitCopyForm(event){
                    this.loading = true;
                    setTimeout(() => {this.loading = false;}, 5000);

                    httpInvoker.post(APIs.alipay.copy,this.copyForm).then(res =>{
                        if(res.status == 200 && res.data.status == '00'){
                            this.showCopy = false;
                            this.$alert('操作成功', '', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    this.initData();
                                }

                            });
                        }else{
                            this.loading = false;
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                },

                submitEditForm(event){
                    var submitData = {
                        id:this.editForm.id,
                        lifeId:this.editForm.lifeId,
                        privteKey:this.editForm.privteKey,
                        publicKey:this.editForm.publicKey,
                        alipayPublicKey:this.editForm.alipayPublicKey,
                        aesKey:this.editForm.aesKey,
                        tel:this.editForm.tel,
                        name:this.editForm.name,
                        akjMerchants:this.editForm.akjMerchants
                    }

                    httpInvoker.post(APIs.alipay.update,submitData).then(res =>{
                        if(res.status == 200 && res.data.status == '00'){
                            this.showEditWin = false;
                            this.$alert('编辑成功', '', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    this.initData();
                                }

                            });
                        }else{
                            this.loading = false;
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    })
                },

                updateLife(row){
                    if(!CommonUtils.checkRole('admin')&&!CommonUtils.checkRole('access')){
                        this.$alert('权限不足', '', {
                            confirmButtonText: '确定',
                        });
                        return;
                    }
                    this.editForm=row;
                    console.log( this.editForm);
                    this.showEditWin = true;
                },
                cancelEdit(event){
                    this.showEditWin = false;
                    this.initData();
                },
                deleteLife(row){
                    if(!CommonUtils.checkRole('admin')&&!CommonUtils.checkRole('access')){
                        this.$alert('权限不足', '', {
                            confirmButtonText: '确定',
                        });
                        return;
                    }
                    this.$confirm('停用'+row.name+'，会作废'+row.name+'下面的所有订单，订单作废后无法恢复！确定要停用吗？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        httpInvoker.post(APIs.alipay.delete,{id:row.id}).then(res =>{
                            if(res.status == 200 && res.data.status == '00'){
                                this.$alert('停用成功', '', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        this.initData();
                                    }

                                });
                            }else{
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        })
                    }).catch(() => {

                    });
                },openCopy(obj){
                    this.showCopy=true;
                    this.$set(this.copyForm,'copyFrom',obj.id);
                    this.$set(this.copyForm,'name',obj.name);
                    this.$set(this.copyForm,'tel',obj.tel);
                }
            },
            mounted: function () {
                this.initData();
            },
        })
    })
})